package Blog;



import org.apache.commons.io.FileUtils;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.CsvFileSource;
import org.junit.jupiter.params.provider.MethodSource;
import org.openqa.selenium.*;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.io.IOException;
import java.time.Duration;
import java.util.concurrent.TimeUnit;
import java.util.stream.Stream;

import static java.lang.Thread.sleep;

public class BlogCases extends  InitAndEnd{


    public static Stream<Arguments> Generator() {
        return Stream.of(Arguments.arguments("http://8.138.132.164:8080/blog_content.html?blogId=7",
                "博客正文",
                "body > div.container > div > div:nth-child(1) > div.title"));
    }

    /**
     * 输入正确的账号密码，登录成功
     */
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void LoginSuccess(String username, String password,String blog_list_url){
        driver.get("http://8.138.132.164:8080/blog_login.html");
        driver.findElement(new By.ByCssSelector("#username")).sendKeys(username);
        driver.findElement(new By.ByCssSelector("#password")).sendKeys(password);
        driver.findElement(new By.ByCssSelector("#submit")).click();

        //跳转到登录页面url，并且获取其url，如果和目标路径url相同，则通过
        String cur_url = driver.getCurrentUrl();
        Assertions.assertEquals(blog_list_url,cur_url);

        System.out.println("验证通过");
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.MINUTES);
    }


    @ParameterizedTest
    @CsvFileSource(resources = "LoginFail.csv")
    void  LoginFail(String username,String password,String blog_list_url) throws InterruptedException, IOException {
        driver.get("http://8.138.132.164:8080/blog_login.html");
        driver.findElement(new By.ByCssSelector("#username")).sendKeys(username);
        driver.findElement(new By.ByCssSelector("#password")).sendKeys(password);
        driver.findElement(new By.ByCssSelector("#submit")).click();

        Alert alert = driver.switchTo().alert();
        sleep(3000);
//        File file  = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
//        FileUtils.copyFile(file,new File("D://2024_6_13jietu.png"));

        alert.accept();
        String cur_url = driver.getCurrentUrl();
        Assertions.assertNotEquals(blog_list_url,cur_url);
        System.out.println("测试密码错误，登录失败，验证通过");
    }


    /**
     * 博客列表页博客数量不为0
     */
    @ParameterizedTest
    @CsvFileSource(resources = "LoginSuccess.csv")
    void BlogList(String username,String password) {
        // 打开博客列表页
        driver.get("http://8.138.132.164:8080/blog_login.html");
        driver.findElement(new By.ByCssSelector("#username")).sendKeys(username);
        driver.findElement(new By.ByCssSelector("#password")).sendKeys(password);
        driver.findElement(new By.ByCssSelector("#submit")).click();
        // 获取页面上所有博客标题对应的元素
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        int title_num = driver.findElements(By.cssSelector(".title")).size();
        // 如果元素数量不为0，测试通过
        Assertions.assertNotEquals(0 ,title_num);
        System.out.println("测试显示博客列表通过");
    }
    /**
     * 博客详情
     */
    @ParameterizedTest
    @MethodSource("Generator")
    void BlogDeatil(String expected_url,String expected_title,String expected_blog_title){
        driver.get("http://8.138.132.164:8080/blog_login.html");
        driver.findElement(new By.ByCssSelector("#username")).sendKeys("lisi");
        driver.findElement(new By.ByCssSelector("#password")).sendKeys("123456");
        driver.findElement(new By.ByCssSelector("#submit")).click();
        driver.manage().timeouts().implicitlyWait(3,TimeUnit.SECONDS);
        driver.findElement(By.xpath("/html/body/div[2]/div/div[1]/a")).click();
        //页面路径
        String currentUrl = driver.getCurrentUrl();
        //页面标题
        String title = driver.getTitle();
//        //博客标题
//        String blogtitle = driver.findElement(new By.ByCssSelector("body > div.container > div > div:nth-child(1) > div.title")).getText();
        Assertions.assertEquals(currentUrl, expected_url);
        Assertions.assertEquals(title,expected_title);
//        Assertions.assertEquals(expected_blog_title,blogtitle);
        System.out.println("博客详情页测试通过");
    }

    /**
     *
     * 写博客
     */

    @Test
    void EditBlog() throws InterruptedException {
        driver.get("http://8.138.132.164:8080/blog_login.html");
        driver.findElement(new By.ByCssSelector("#username")).sendKeys("lisi");
        driver.findElement(new By.ByCssSelector("#password")).sendKeys("123456");
        driver.findElement(new By.ByCssSelector("#submit")).click();
        // 找到写博客按钮,点击
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(5)")).click();
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 通过Js讲鼻涕进行输入
        ((JavascriptExecutor)driver).executeScript("document.getElementById(\"title1\").value=\"自动化测试\"");
        sleep(3000);
        // 点击发布
        driver.findElement(By.cssSelector("body > div.blog-edit-container > div.title > button")).click();
        sleep(3000);
        // 获取当前页面url
        String cur_url = driver.getCurrentUrl();
        Assertions.assertEquals("http://8.138.132.164:8080/blog_list.html", cur_url);
        System.out.println("编写博客功能测试通过");
    }

    /**
     *
     * 删除和刚才发布的博客
     */
    @Test
    void DeleteBlog() throws InterruptedException {
        // 打开博客列表页面
        driver.get("http://8.138.132.164:8080/blog_login.html");
        driver.findElement(new By.ByCssSelector("#username")).sendKeys("lisi");
        driver.findElement(new By.ByCssSelector("#password")).sendKeys("123456");
        driver.findElement(new By.ByCssSelector("#submit")).click();
        // 点击全文按钮
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        driver.findElement(By.cssSelector("body > div.container > div > div:nth-child(1) > a")).click();
        // 点击删除按钮
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        driver.findElement(By.cssSelector("#content > div > button:nth-child(2)")).click();
        sleep(3000);
        driver.switchTo().alert().accept();
        // 博客列表页第一篇博客标题不是“自动化测试”
        String first_blog_title = driver.findElement(By.xpath("/html/body/div[2]/div[2]/div/h3/div")).getText();
        // 校验当前博客标题不等于“自动化测试”
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        Assertions.assertNotEquals(first_blog_title, "自动测试");
        System.out.println("删除博客测试通过");
    }

    /**
     * 注销
     */
    @Test
    void Logout() {
        driver.get("http://8.138.132.164:8080/blog_login.html");
        driver.findElement(new By.ByCssSelector("#username")).sendKeys("lisi");
        driver.findElement(new By.ByCssSelector("#password")).sendKeys("123456");
        driver.findElement(new By.ByCssSelector("#submit")).click();
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(6)")).click();
        driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
        // 校验url(登录url)
        String cur_url = driver.getCurrentUrl();
        Assertions.assertEquals("http://8.138.132.164:8080/blog_login.html", cur_url);
        // 校验提交按钮
        WebElement webElement = driver.findElement(By.cssSelector("#submit"));
        Assertions.assertNotNull(webElement);
        System.out.println("注销测试通过");
    }
}
